package light.head.controller.inv;

import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import light.head.constant.Msg;
import light.head.constant.Parm;
import light.head.constant.Url;
import light.head.dao.inv.InvAccountDAO;
import light.head.dao.inv.InvAccountTypeDAO;
import light.head.dao.sys.OrganDAO;
import light.head.enm.RESULT;
import light.head.framework.base.SupporterController;
import light.head.framework.entity.PageBean;
import light.head.model.inv.InvAccount;
import light.head.model.inv.InvAccountType;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
@Controller
@RequestMapping(value = Url.C_INVACCOUNT)
public class InvAccountController extends SupporterController{
	private InvAccountDAO iaDAO;
	private OrganDAO organDAO;
	private InvAccountTypeDAO iatDAO;
	
	@RequestMapping(value = Url.INIT)
	public ModelAndView init(HttpServletRequest request, HttpServletResponse response){
		return new ModelAndView(Url.INVACCOUNT_INIT);
	}
	@RequestMapping(value = Url.QUERY)
	public ModelAndView query(HttpServletRequest request, HttpServletResponse response){
		PageBean pb = createPageBean(request);
		Map<String, String> map = createQueryMapByParmName(request, Parm.CARD, Parm.ON, Parm.CN, Parm.TIME1, Parm.TIME2, Parm.DELETED);
		Integer oc = organDAO.getRoot(cu_organCode(request)).getCode();
		if (iaDAO.findByMap(oc, map, pb) == RESULT.SUCCESS) {
			request.setAttribute(Parm.PAGE_BEAN, pb);
		}
		return new ModelAndView(Url.INVACCOUNT_LIST);
	}
	
	
	@RequestMapping(value = Url.EDIT)
	public ModelAndView edit(HttpServletRequest request, HttpServletResponse response){
		String code = request.getParameter(Parm.CODE);
		if (null != code) {
			InvAccount ia = iaDAO.get(Integer.parseInt(code));
			request.setAttribute(Parm.INVACCOUNT, ia);
		}
		Integer oc = organDAO.getRoot(cu_organCode(request)).getCode();
		List<InvAccountType> typeList = iatDAO.findAll(oc);
		if (null != typeList && typeList.size() > 0) {
			StringBuffer result = new StringBuffer("[");
			
			for (InvAccountType type : typeList){
				result.append("{");
				result.append("\"atn\":\"" + type.getName() +"\",");
				result.append("\"atc\":\"" + type.getCode() +"\"");
				result.append("},");
			}
			result.deleteCharAt(result.length() - 1).append("]");
			request.setAttribute(Parm.TYPE, result.toString());
		} 
		return new ModelAndView(Url.INVACCOUNT_EDIT);
	}
	
	@RequestMapping(value = Url.SAVE)
	public void save(InvAccount ia, HttpServletRequest request, HttpServletResponse response) throws Exception{
		if (ia == null) {
			print(response, Msg.PARAMETER_ERROR);
			return ;
		}
		RESULT result = RESULT.ERROR;
		if (ia.getCode() == null) {
			Integer oc = organDAO.getRoot(cu_organCode(request)).getCode();
			List<InvAccountType> iac = iatDAO.findByNameOc(ia.getAtn(), oc);
			if (iac == null || iac.size() == 0)  {
				InvAccountType atn = new InvAccountType();
				atn.setCu(cu_code(request));
				atn.setCt(new Date());
				atn.setDeleted(false);
				atn.setName(ia.getAtn());
				atn.setOc(oc);
				iatDAO.add(atn);
				ia.setAtc(atn.getCode());
			}
			ia.setOc(oc);
			ia.setCu(cu_code(request));
			ia.setCt(new Date());
			ia.setDeleted(false);
			result = iaDAO.add(ia);
		} else {
			Integer oc = organDAO.getRoot(cu_organCode(request)).getCode();
			List<InvAccountType> iac = iatDAO.findByNameOc(ia.getAtn(), oc);
			if (iac == null || iac.size() == 0)  {
				InvAccountType atn = new InvAccountType();
				atn.setCu(cu_code(request));
				atn.setCt(new Date());
				atn.setDeleted(false);
				atn.setName(ia.getAtn());
				atn.setOc(cu_organCode(request));
				iatDAO.add(atn);
				ia.setAtc(atn.getCode());
			}
			result = iaDAO.update(ia);
		} if (RESULT.SUCCESS == result) {
			print(response, Msg.SUCCESS);
		} else
			print(response, Msg.ERROR);
	}
	
	
	
	
	
	
	
	@RequestMapping(value = Url.SHIELD)
	public void shield(HttpServletRequest request, HttpServletResponse response) throws Exception {
		String code = request.getParameter(Parm.CODE);
		if (!isNotNull(code)) {
			print(response, Msg.PARAMETER_ERROR);
			return;
		}
		InvAccount ia = iaDAO.get(Integer.parseInt(code));
		if (null == ia) {
			print(response, Msg.NOT_FIND);
			return;
		}
		ia.setDeleted(true);
		if (iaDAO.upd(ia) == RESULT.SUCCESS)
			print(response, Msg.SUCCESS);
		else
			print(response, Msg.ERROR);
	}
	
	@RequestMapping(value = Url.UN_SHIELD)
	public void unshield(HttpServletRequest request, HttpServletResponse response) throws Exception {
		String code = request.getParameter(Parm.CODE);
		if (!isNotNull(code)) {
			print(response, Msg.PARAMETER_ERROR);
			return;
		}
		InvAccount ia = iaDAO.get(Integer.parseInt(code));
		if (null == ia) {
			print(response, Msg.NOT_FIND);
			return;
		}
		ia.setDeleted(false);
		if (iaDAO.upd(ia) == RESULT.SUCCESS)
			print(response, Msg.SUCCESS);
		else
			print(response, Msg.ERROR);
	}
	/**
	 * 查询账户类型
	 */
	@RequestMapping(Url.AID_INIT)
	public ModelAndView aidOrganInit(HttpServletRequest request, HttpServletResponse response) throws Exception {
		return new ModelAndView(Url.INVACCOUNT_AID_INIT);
	}
	
	@RequestMapping(Url.AID_LIST)
	public ModelAndView aidOrganList(HttpServletRequest request, HttpServletResponse response) throws Exception {
		PageBean pageBean = createPageBean(request);
		Map<String, String> map = createQueryMapByParmName(request, Parm.OC, Parm.NAME);
		Integer oc = organDAO.getRoot(cu_organCode(request)).getCode();
		if (RESULT.SUCCESS == iatDAO.findByMap(oc, map, pageBean)) {
			request.setAttribute(Parm.PAGE_BEAN, pageBean);
		}
		return new ModelAndView(Url.INVACCOUNT_AID_LIST);
	}
	
	@Autowired
	public void setIaDAO(InvAccountDAO iaDAO) {
		this.iaDAO = iaDAO;
	}
	@Autowired
	public void setOrganDAO(OrganDAO organDAO) {
		this.organDAO = organDAO;
	}
	@Autowired
	public void setIatDAO(InvAccountTypeDAO iatDAO) {
		this.iatDAO = iatDAO;
	}
	
	
}
